package zuoye5;


import java.util.*;

import java.util.ArrayList;
import java.util.List;
public class StudentMgr {
    //用来保存学生对象
    private static List<Student> studentList = new ArrayList<>();

    //新增学生
    public static boolean addStudent(Student student) {
        for (int index = 0; index < studentList.size(); index++) {
            Student stu = studentList.get(index);
            if (stu.getId() == student.getId()) {
                return false;
            }
            return studentList.add(student);
        }
        return true;
    }

    //根据学号(唯一性)删除集合中指定的学生对象
    public static boolean delStudentById(int id) {
        int index = -1;
        for (int i = 0; i < studentList.size(); i++) {
            Student student = studentList.get(i);
            if (id == student.getId()) {
                index = i;
                break;
            }
        }
        if (index != -1) {
            studentList.remove(index);
            return true;
        } else {
            return false;
        }
    }

    //根据学号修改学生信息
    public static boolean updStudentById(Student student) {
        int index = -1;
        for (int i = 0; i < studentList.size(); i++) {
            Student stu = studentList.get(i);
            if (stu.getId() == student.getId()) {
                index = i;
                break;
            }
        }
        if (index != -1) {
            studentList.set(index, student);
            return true;
        } else {
            return false;
        }
    }

    //返回所有的学生对象
    public static List<Student> getAllStudent() {
        return studentList;
    }

    //根据学号获取单个学生详细信息
    public Student getStudentById(int id) {
        int index = -1;
        for (int i = 0; i < studentList.size(); i++) {
            Student student = studentList.get(i);
            if (id == student.getId()) {
                index = i;
                break;
            }
        }
        if (index != -1) {
            return studentList.get(index);
        } else {
            return null;
        }
    }

    //根据学生姓名模糊查询
    public List<Student> searchStudentByLike(String name) {
        List<Student> tempList = new ArrayList<>();
        for (Student student : studentList) {
            if (student.getName().indexOf(name) != -1) {
                tempList.add(student);
            }
        }
        return tempList;
    }

    //应用程序入口方法
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        boolean flag = true;
        do {
            System.out.println("===学生信息管理系统===");
            System.out.println("1.增加学生信息");
            System.out.println("2.修改学生信息");
            System.out.println("3.删除学生信息");
            System.out.println("4.显示所有学生");
            System.out.println("5.根据学号查询");
            System.out.println("6.根据姓名查询");
            System.out.println("0.退出");
            System.out.println("=====================");
            System.out.println("请选择:");
            int choice = input.nextInt();
            switch (choice) {
                case 1:
                    System.out.println("请输入学号:");
                    int id = input.nextInt();
                    System.out.println("请输入姓名:");
                    String name = input.next();
                    System.out.println("请输入年龄:");
                    int age = input.nextInt();
                    System.out.println("请输入性别:");
                    String sex = input.next();
                    Student student = new Student(id, name, age, sex);
                    boolean result = addStudent(student);
                    if (result) {
                        System.out.println("新增学生信息成功！");
                    } else {
                        System.out.println("新增学生信息失败！");
                    }
                    break;
                case 4:
                    List<Student> students = getAllStudent();
                    Iterator<Student> it = students.iterator();
                    System.out.println("学号\t\t姓名\t\t年龄\t\t");
                    System.out.println("-----------------------------------------");
                    while (it.hasNext()) {
                        Student stu = it.next();
                        System.out.println(stu.getId() + "\t" + stu.getName() + "\t\t" + stu.getAge() + "\t\t" );
                        System.out.println("-----------------------------------------");
                    }
                    break;
                case 0:
                    System.out.println("谢谢使用本系统，欢迎下次光临！");
                    flag = false;
                    break;
                default:
                    System.out.println("你输入的序号无效，请重新输入");
                    flag = true;
                    break;
            }
        } while (flag);
    }
}
